// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cn.crc.pjblog.components.table;


import org.apache.tapestry5.Asset;
import org.apache.tapestry5.ComponentResources;

import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Path;
import org.apache.tapestry5.annotations.Persist;

import org.apache.tapestry5.corelib.base.AbstractField;

import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.services.FormSupport;
import org.apache.tapestry5.services.Request;

/**
 * 分页的页面导航显示组件
 * @author doublel
 * @version 0.0.1
 *
 * 
 */
public class TablePages extends AbstractField{
	
	@Inject 
	@Path("${componentimages}/last01.gif")
	private Asset last01;

	@Inject 
	@Path("${componentimages}/last02.gif")
	private Asset last02;
	
	@Inject 
	@Path("${componentimages}/first01.gif")
	private Asset first01;	
//	
	@Inject 
	@Path("${componentimages}/first02.gif")
	private Asset first02;	
	
	@Inject	
	private ComponentResources componentResources;
	
	private boolean showFirstPrvBoolean;
	private boolean showNextLastBoolean;
	
	
	private long totalPage =0;
	
	
	
	/**
	 * 当前所在的页数默认值为1
	 * 
	 * */
	
	@Persist("client")
	@Parameter(value="1")
	private int curPageNum;
	
	/**
	 * 得到所有日志的数量
	 * 默认是值为0
	 * 
	 */
	@Parameter(value="0")
	private long totalRowCount;
	
//	@Component(parameters = { "source=pageNums", "value=pageIndex" })
//	private Loop splitPageLoop;
	/**
	 * 每页显示的行数，默认为10
	 */
	@Parameter(value="1")
	private int nPageSize;
	
//	@Component(parameters ={"context=curPageNum","id=literal:splitPage"})
//	private ActionLink actionLinkFirst01;
	/**
	 * 分页时页码使用的循环组件
	 */
//	@Component(parameters = { "source=pageNums", "value=pageIndex" })
//	private Loop splitPageLoop;
	
	@Inject
	private Messages  _message;
	
	@Inject
	private Request _request;
	
		
	/**
	 * component initialize method 
	 * 在这里根据页面传过来的所有记录条数，和每一页的条数
	 * 计算分页的所有页数 totalPage 
	 * 
	 * @return
	 * 
	 */
	public void setupRender(){

		long totalCount = this.totalRowCount;
		
		if(totalCount%nPageSize==0){
			totalPage = totalCount/nPageSize;
		} else{
			totalPage = totalCount/nPageSize+1;
		}
		
//		System.out.println("setupRender this.curPageNum="+this.curPageNum);
//		System.out.println("======setupRender执行=========");
//		System.out.println("setupRender +tempintValue"+ this.tempintValue);
	}
	
	public void afterRender(){
//		this.setCurPageNum(this.tempintValue);
//		System.out.println("afterRender +tempintValue"+ this.tempintValue);
		
	}	
	
	public boolean getShowFirstPrvBoolean(){
		if(this.curPageNum == 1){
			return true;
		}else
		{
			return false;
		}
		
	}
	
	
	public boolean getShowNextLastBoolean(){
		
		if (this.curPageNum>= this.totalPage){
			return true;
		}else
		{
		
			return false;
		}
	}
	
	

	/* (non-Javadoc)
	 * @see org.apache.tapestry.corelib.base.AbstractField#processSubmission(org.apache.tapestry.services.FormSupport, java.lang.String)
	 */
//	@Override
//	protected void processSubmission(FormSupport formSupport, String elementName) {
//		// TODO Auto-generated method stub
//		//需要获得当前的context然后付给当前页面
//		
////		String rawValue = _request.getParameter(elementName);
////		System.out.println("提交的zhegeelementName=="+elementName);
////		System.out.println("值rawValue=="+rawValue);
////		String pageInput = _request.getParameter("pageInput");
////		System.out.println("pageInput="+pageInput);		
////		String curPageNumHidden = _request.getParameter("curPageNumHidden");
////		System.out.println("curPageNumHidden=="+curPageNumHidden);
////		if (curPageNumHidden == null || curPageNumHidden.trim().length()==0 ) curPageNumHidden="1";
//		
////		this.setCurPageNum(Integer.valueOf(curPageNumHidden));
////		tempintValue = this.getCurPageNum();
//		
////		System.out.println("curPageNumHidden=="+curPageNumHidden);
////		System.out.println("this.curpagenaum="+this.getCurPageNum());
////		this.curPageNum = ;
//	}

	

	/* (non-Javadoc)
	 * @see org.apache.tapestry.corelib.base.AbstractField#processSubmission(java.lang.String)
	 */
	@Override
	protected void processSubmission(String arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @return Returns the totalRowCount.
	 */
	public long getTotalRowCount() {
		return totalRowCount;
	}

	/**
	 * @param totalRowCount The totalRowCount to set.
	 */
	public void setTotalRowCount(long totalRowCount) {
		this.totalRowCount = totalRowCount;
	}

	/**
	 * @return Returns the nPageSize.
	 */
	public int getNPageSize() {
		return nPageSize;
	}

	/**
	 * @param pageSize The nPageSize to set.
	 */
	public void setNPageSize(int pageSize) {
		nPageSize = pageSize;
	}

	/**
	 * @return the first01
	 */
	public Asset getFirst01() {
		return first01;
	}

	/**
	 * @return the first02
	 */
	public Asset getFirst02() {
		return first02;
	}

	/**
	 * @return the last01
	 */
	public Asset getLast01() {
		return last01;
	}

	/**
	 * @return the last02
	 */
	public Asset getLast02() {
		return last02;
	}

	/**
	 * @param first01 the first01 to set
	 */
	public void setFirst01(Asset first01) {
		this.first01 = first01;
	}

	/**
	 * @param first02 the first02 to set
	 */
	public void setFirst02(Asset first02) {
		this.first02 = first02;
	}

	/**
	 * @param last01 the last01 to set
	 */
	public void setLast01(Asset last01) {
		this.last01 = last01;
	}

	/**
	 * @param last02 the last02 to set
	 */
	public void setLast02(Asset last02) {
		this.last02 = last02;
	}

	/**
	 * @return the curPageNum
	 */
	public int getCurPageNum() {
		return this.curPageNum;
	}

	/**
	 * @param curPageNum the curPageNum to set
	 */
	public void setCurPageNum(int curPageNum) {
		this.curPageNum = curPageNum;
	}

	

	/**
	 * @return the totalPage
	 */
	public long getTotalPage() {
		return totalPage;
	}

	
	/**
	 * @param totalPage the totalPage to set
	 */
	public void setTotalPage(long totalPage) {
		this.totalPage = totalPage;
	}
	
	/////////////////----Bean Proptetyis=====/////////////////
	
}
